<%@ page import="org.apache.struts.action.Action"%>
<%@page import="com.mbbmap.util.StringUtils"%>
<%@ page import = "java.io.*" %>
<%@ page import = "java.net.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.util.Map" %>
<%@ page import = "java.util.HashMap" %>
<%@ page import="com.mbbmap.app.manager.*" %>
<%@ page import="com.mbbmap.app.EbppKeys" %>
<%@ page import="com.mbbmap.util.*" %>
<%@ page import = "com.mbbmap.security.dao.*" %>

<%
String msg = StringUtils.NullSafe((String) request.getAttribute("msg"));
String msg_id = StringUtils.NullSafe((String) request.getAttribute("msg_id"));

String userid = StringUtils.NullSafe((String) request.getAttribute("userid"));
String pass1 = StringUtils.NullSafe((String) request.getAttribute("pass1"));
String pass2 = StringUtils.NullSafe((String) request.getAttribute("pass2"));

boolean showParam = false;
String msgColor = "red";

if (msg_id.equalsIgnoreCase("MSG001") || msg_id.equalsIgnoreCase("MSG002"))
	showParam = true;

if (msg_id.equalsIgnoreCase("MSG999"))
	msgColor = "green";

ArrayList secGroupAccessList = (ArrayList) session.getAttribute(Constants.LOGON_ACCESS_LIST);
SecUserDao oSecUserDao = (SecUserDao) session.getAttribute(Constants.LOGGED_USER);
String sUserName = oSecUserDao.getUserName();
String lastLogonDateTime = (String) session.getAttribute(Constants.LAST_LOGON_DATETIME);

String selected = "sec001";
int iSec001 = 0;
int iSec002 = 0;
int iSec003 = 0;
int iSec004 = 0;
int iSec005 = 0;
int iSec006 = 0;
int iSec007 = 0;
int iSec008 = 0;

for(int i=0;i<secGroupAccessList.size();i++){
	SecAccessDao secaccessDao = (SecAccessDao)secGroupAccessList.get(i);
	String strModuleCode  = secaccessDao.getModuleCode();
	if(strModuleCode.equals("SEC001"))
		iSec001 = 1;
	if(strModuleCode.equals("SEC002"))
		iSec002 = 1;
	if(strModuleCode.equals("SEC003"))
		iSec003 = 1;
	if(strModuleCode.equals("SEC004"))
		iSec004 = 1;
	if(strModuleCode.equals("SEC005"))
		iSec005 = 1;
	if(strModuleCode.equals("SEC006"))
		iSec006 = 1;
	if(strModuleCode.equals("SEC007"))
		iSec007 = 1;
	if(strModuleCode.equals("SEC008"))
		iSec008 = 1;
}


%>

<html>
<head>
<link rel="stylesheet" href="resource/mbb_sec_global.css" type="text/css" />
<link href="resource/style.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
	    window.history.forward();
	    function noBack() { window.history.forward(); }
	</script>
<style type="text/css"></style></head>
<SCRIPT LANGUAGE="JavaScript1.2">
	function AddSecGroup(){
		var sym = /[^A-Za-z0-9]/;
		var sym2 = /[^A-Za-z0-9 ]/;
		strGroupCode = document.AddSecGroupForm.groupcode.value;
		if(strGroupCode==null || strGroupCode==""){
			alert("Please enter Group Code");
			return false;
		}
		else if(strGroupCode.match(sym)){
			alert("No symbols allowed in Group Code");
			return false;
		}
		
		strGroupName = document.AddSecGroupForm.groupname.value;
		if(strGroupName==null || strGroupName==""){
			alert("Please enter Group Name");
			return false;
		}
		else if(strGroupName.match(sym2)){
			alert("No symbols allowed in Group Name");
			return false;
		}
		document.AddSecGroupForm.action='<%=response.encodeURL("UpdateSecGroup.do?dispatch=updgroup")%>';
		document.AddSecGroupForm.submit();
	}

	function AddModuleGroup(){
		var sym = /[^A-Za-z0-9]/;
		var sym2 = /[^A-Za-z0-9\/\ ]/;
		strModuleCode = document.AddSecModuleForm.modulecode.value;
		if(strModuleCode==null || strModuleCode==""){
			alert("Please enter Module Code");
			return false;
		}
		else if(strModuleCode.match(sym)){
			alert("No symbols or spacebar allowed in Module Code");
			return false;
		}

		strModuleName = document.AddSecModuleForm.modulename.value;
		if(strModuleName==null || strModuleName==""){
			alert("Please enter Module Name");
			return false;
		}
		else if(strModuleName.match(sym2)){
			alert("No symbols allowed in Module Name");
			return false;
		}
		document.AddSecModuleForm.action='<%=response.encodeURL("UpdateSecGroup.do?dispatch=updmodule")%>';
		document.AddSecModuleForm.submit();
	}

	function removeModule(strModuleCode, modulecode){
		if(strModuleCode==null || strModuleCode==""){
			alert("Please enter Module Code");
			return false;
		}
		var msg = "Confirm Delete of "+modulecode;
		
		if (confirm(msg)){
			
 		document.updSecModuleForm.sremmodule.value = strModuleCode;
		document.updSecModuleForm.action='<%=response.encodeURL("UpdateSecGroup.do?dispatch=remmodule")%>';
		document.updSecModuleForm.submit();
		}
	}

	function removeGroup(strGroupCode,groupcode){
		if(strGroupCode==null || strGroupCode==""){
			alert("Please enter Group Code");
			return false;
		}
		var msg = "Confirm Delete of "+groupcode;
		
		if (confirm(msg)){
			
 		document.updSecGroupForm.sremgroup.value = strGroupCode;
		document.updSecGroupForm.action='<%=response.encodeURL("UpdateSecGroup.do?dispatch=remgroup")%>';
		document.updSecGroupForm.submit();
		}
	}

</SCRIPT>
</head>
<%
	ArrayList arlSecGroupList = new ArrayList();
	arlSecGroupList = (ArrayList) session.getAttribute(Constants.SECURITY_GROUP);
	ArrayList arlSecModuleList = new ArrayList();
	arlSecModuleList = (ArrayList) session.getAttribute(Constants.SECURITY_MODULE);
%>

<body class="home" onload="noBack();" onpageshow="if (event.persisted) noBack();" onunload="">

	<a name="top"></a>
	
<!-- Header -->
<%@ include file="/jsp/common/Header.jsp" %>	
<!-- End Header -->

<div id="bgWrapper">
		<div id="container">
		
					<div id="titleBar">
						<ul class="topBar">
							<li class="goBackBt"><a href="<%=request.getContextPath()%>/ShowSecurityHome.do?dispatch=init"></a></li>

							<li>
							  <h1>Groups & Modules Setup</h1></li>
						</ul>
					</div>
		
					<div id="fullcontent">
		
						<!-- Security Navigation -->
						<%@ include file="SecurityNavigation.jsp" %>	
						<!-- End SecurityNavigation -->
			
			
						<div id="contentRightIn">
									
							<div style="background-color:#d6d6d8;" id="moduleContainer">
								
								<!-- Module Title -->
								<table align="center" width=100% border="0" rules="cols" class="tblDetails">
										<thead>
											<tr>
												<th colspan="4">
													<h4>Security Groups & Modules</h4>
												</th>
											</tr>
										</thead>
											
										<tr class="even">
											<td colspan="2" align="center" style="text-align:center !important;">
												Security Module - Update Security Group & Modules.
												<p style="font-weight:bold;color: <%=msgColor%> !important;"><%=msg%></p>
											</td>
										</tr>
								</table>
							
							
								<br />
							
								<form name="updSecGroupForm" action="" method="post">
									<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
									<input type="hidden" name="sremgroup" value="">
									<table align="center" class="tbldetails" width=95% border="0" rules="cols">
										<thead class="th1">
											<tr>
												<th colspan="4">
													<div class="sectionTableTitle">
														<h2 class="modTitle">Security Groups</h2>
													</div>
												</th>
										</thead>
										<tr class="headerD" style="border: thin solid">
											<td width="40%" class="center_note">Group Code</td>
											<td width="40%" class="center_note">Group Name</td>
											<td width="20%" class="center_note">Remove</td>
										</tr>
											<%
											String row = "";
											int result;
											for(int i=0;i<arlSecGroupList.size();i++){
												result = (i + arlSecGroupList.size()) % 2;
									
												if(result==0)
												{
													row="even";
												}
												else
												{
													row="odd";
												}
									
												SecGroupsDao secgroupDao = (SecGroupsDao) arlSecGroupList.get(i);
												String strEGroupCode  = EncryptionHelper.encrypt(secgroupDao.getGroupCode(),ConfigManager.getInstance().get(EbppKeys.ENCRYPTION_PASSPHRASE));
												%>
															<tr class="<%=row%>" >
																<td width="40%" class="center_note"><%=secgroupDao.getGroupCode()%></td>
																<td width="40%" class="center_note"><%=secgroupDao.getGroupName()%></td>										
																<td width="20%" class="center_note"><a href="#" onclick="removeGroup('<%=strEGroupCode%>','<%=secgroupDao.getGroupCode()%>');overlay();"><font color="#0033cc"><u>Remove</u></font></a></td>
															</tr>
												<%
														}
												%>
									</table>
								</form>
								
								<br />
					
								<form name="AddSecGroupForm" action="" method="post">
									<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
									<table align="center" border=0 width = 95%>
										<tr>
											<td width="15%" >Group Code : </td>
											<td width="30%" ><input type="text" name="groupcode" value="" size=20 maxlength="10"></td>
											<td width="15%" >Group Name: </td>
											<td width="30%" ><input type="text" name="groupname" value="" size=20 maxlength="20"></td>
											<td width="10%" ><input type="button" value="Submit" onClick="AddSecGroup()"> </td>
										</tr>
								  	</table>
								</form>
				
								<hr />
								<br />
								<form name="updSecModuleForm" action="" method="post">
									<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
									<input type="hidden" name="sremmodule" value="">
									<table align="center" class="tbldetails" width=95% border="0" rules="cols" >
										<thead class="th1">
											<tr>
												<th colspan="7">
													<div class="sectionTableTitle">
														<h2 class="modTitle">Security Modules</h2>
													</div>
												</th>
										</thead>
										<tr class="headerD" style="border: thin solid">
											<td width="40%" class="center_note">Security Modules</td>
											<td width="40%" class="center_note">Modules Name</td>
											<td width="20%" class="center_note">Remove</td>
										</tr>
							
										<%
												String mrow = "";
												int mresult;
												for(int i=0;i<arlSecModuleList.size();i++){
													mresult = (i + arlSecModuleList.size()) % 2;
										
													if(mresult==0)
													{
														mrow="even";
													}
													else
													{
														mrow="alt";
													}
										
													SecModulesDao secmoduleDao = (SecModulesDao)arlSecModuleList.get(i);
													String strEModuleCode  = EncryptionHelper.encrypt(secmoduleDao.getmoduleCode(),ConfigManager.getInstance().get(EbppKeys.ENCRYPTION_PASSPHRASE));
										%>
													<tr class="<%=mrow%>" >
														<td width="40%" class="center_note"><%=secmoduleDao.getmoduleCode()%></td>
														<td width="40%" class="center_note"><%=secmoduleDao.getmoduleName()%></td>
														<td width="20%" class="center_note"><a href="#" onclick="removeModule('<%=strEModuleCode%>','<%=secmoduleDao.getmoduleCode()%>');overlay();"><font color="#0033cc"><u>Remove</u></font></a></td>
													</tr>
										<%
												}
										%>
							
									</table>
								</form>
						
						
						<br />
						<br />
					
						<form name="AddSecModuleForm" action="" method="post">
							<input type="hidden" name="<%= Constants.TOKEN_KEY %>" value="<%= session.getAttribute(Action.TRANSACTION_TOKEN_KEY) %>" >
							<table align="center" border=0 width = 95%>
								<tr>
									<td width="15%" >Module Code : </td>
									<td width="30%" ><input type="text" name="modulecode" value="" size=20 maxlength="10"></td>
									<td width="15%" >Module Name: </td>
									<td width="30%" ><input type="text" name="modulename" value="" size=20 maxlength="30"></td>
									<td width="10%" ><input type="button" value="Submit" onClick="AddModuleGroup()"> </td>
								</tr>
						  	</table>
						</form>
				
				
							<div id="contentMessage">
								<h3 class="<%=msgColor%>"><%=msg%></h3>
							</div>
							
						</div>
						</div> <!--  moduleContainer END -->
					</div>
		
					<!-- Footer -->
					<%@ include file="/jsp/common/Footer.jsp" %>	
					<!-- End Footer -->
					</div>




</body>
</html>